package com.eascs.web.o2o.web.reward.controller;

import java.text.DecimalFormat;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.eascs.app.dao.PageResult;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.controller.BaseRestController;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.PaginationRestResult;
import com.eascs.site.view.ViewContext;
import com.eascs.web.context.RequestContext;
import com.eascs.web.o2o.web.reward.entity.CurLoanDetailForm;
import com.eascs.web.o2o.web.reward.service.CurLoanDetailService;

@RestController
@RequestMapping(value="/restCurLoanDetail")
public class RewardCurLoanDetailRestController extends BaseRestController{
	
	@Autowired
	CurLoanDetailService curLoanDetailService;

	@RequestMapping(value="/curLoanDetailList")
	private PaginationRestResult loanDetailList(CurLoanDetailForm entity) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getCheckMonth()==null?date:entity.getCheckMonth();
		entity.setCheckMonth(month);
		String o2oUrl = "/restLoanDetail/loanDetailList";
		if(entity != null) {
			String loanDateStart = entity.getLoanDateStart();
			String loanDateEnd = entity.getLoanDateEnd();
			String checkMonth = entity.getCheckMonth();
			String provinceName = entity.getProvinceName();
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "loanDateStart", loanDateStart);
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "loanDateEnd", loanDateEnd);
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "checkMonth", checkMonth);
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "provinceName", provinceName);
			o2oUrl = o2oUrl + "&pageIndex=";
		}
		entity.setPageSize(10);
		PageResult<Object[]> pageResult = curLoanDetailService.getCurLoanDetailList(entity, "search");
		String[] titles = {"考核月份", "省区", "被考核对象工号", "被考核对象姓名", "贷款单号", "借款人", "放款金额（万元）", "放款期限", "折算后放款金额(万元)", "放款日期", "到期日期", "信贷经理", "推荐人", "推荐类型", "推荐人分佣系数", "是否续贷", "被考核对象折算金额(万元)", "被考核对象与本单关系"};
		PaginationInfo paginationInfo = new PaginationInfo(pageResult, titles, UrlUtil.resetParam(RequestContext.getRequestUrlWithQueryString(), "pageIndex", ""));
		paginationInfo.setFirstRenderColumnIndex(0);
		paginationInfo.setFormats(new Format[] {null, null, null, null, null, null, new DecimalFormat(",##0.00"), null, new DecimalFormat(",##0.00"), null, null, null, null, null, null, null, new DecimalFormat(",##0.00"), null});
		paginationInfo.setRendTo("#o2o_result");
		ViewContext.put("vo", entity);
		ViewContext.put("o2oUrl", o2oUrl);
		return PaginationRestResult.buildPaginationRestResult(paginationInfo);
	}
}
